(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:NotifyCtrl
     * @description
     * # NotifyCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('NotifyCtrl', NotifyCtrl);

    NotifyCtrl.$inject = ['$scope', 'dataService', 'helper', '$location'];

    function NotifyCtrl($scope, dataService, helper, $location) {

        $scope.listAction = function() {
            $scope.xtotal = 0;
            $scope.pageSize = 10;
            $scope.getList();
        }

        //获取仓库列表
        $scope.getList = function(pageNo) {
            $scope.pageNo = pageNo || 1;
            var query = {
                page_no: $scope.pageNo,
                page_size: $scope.pageSize
            };

            if ($scope.notify_id) {
                query.query = { "notify_id": $scope.notify_id };
            }
            dataService.notifyGet(query).then(function(list) {
                if (list && list.notifys) {
                    $scope.notifys = list.notifys;
                    $scope.xtotal = list.total_count;
                }
            });
        }


        $scope.addAction = function() {
            var notify_id = $location.search()["notify_id"];
            if (notify_id) {
                dataService.notifyOne(notify_id).then(function(result) {
                    if (result.notify_id) {
                        $scope.notify = result;
                    } else {
                        helper.myAlert("错误：" + result.Msg);
                    }
                })
            } else {
                $scope.notify = {
                    type_id: 1,
                    store_id: -1,
                    start_at: "",
                    end_at: "",
                    content: "这是一个测试通知内容",
                }
            }
        }

        $scope.save = function() {
            var method = $scope.notify.notify_id ? "isvNotifyUpdate" : "isvNotifyAdd";
            if ($scope.notify.type_id == 2 && !$scope.notify.store_id) {
                return helper.myAlert("门店ID必须设置");
            }
            dataService[method]($scope.notify).then(function(result) {
                console.log("------", result);
                if (result.Msg) {
                    helper.myAlert("错误：" + result.Msg);
                } else {
                    gourl("#/notify/list");
                }
            });
        }

        $scope.delete = function(notify_id) {
            dataService.isvNotifyDelete(notify_id).then(function(result) {
                console.log("------", result);
                if (result.Msg) {
                    helper.myAlert("错误：" + result.Msg);
                } else {
                    $scope.getList();
                }
            });
        }
    }
})();
